package com.maxmlr.music.service;

import com.maxmlr.music.dao.PictureMapper;
import com.maxmlr.music.dao.UserMapper;
import com.maxmlr.music.model.Picture;
import com.maxmlr.music.model.User;
import com.maxmlr.music.util.Md5Util;
import com.maxmlr.music.util.Tool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户业务处理层
 *
 * @author maxiao
 */
@Slf4j
@Service
public class UserService extends FathService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PictureMapper pictureMapper;

    /**
     * 用户业务层重写方法，调用
     *
     * @param sendMap
     *            用户上送字段
     * @return ret 返回字段
     */
//	@Override
//	public Map<String, String> fservice(Map<String, String> sendMap) {
//		if (sendMap == null) {
//			throw new RuntimeException(ERRMSG);
//		}
//		Map<String, String> ret = new HashMap<String, String>();
//
//		return ret;
//	}

    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    public Map<String, Object> saveUser(User user) {
        if (user == null) {
            throw new RuntimeException(ERRMSG);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        Integer id = user.getUser_id();
        Picture picture = pictureMapper.findUserById(id);
        if (picture == null) {
            map.put(FLAG, FALSE);
        } else {
            picture.setTemperature(picture.getTemperature() + 1);
            pictureMapper.update(picture);
            user.setPassword(Md5Util.md5(user.getPassword()));
            user.setUser_id(0);
            /**
             * 用户IP
             */
            user.setLogin_ip(Tool.getRemoteHost());
            /**
             * 为保持数据库不为null,对参数进行处理
             */
            userMapper.saveUser(setUserDellNull(user));
            map.put(FLAG, TRUE);
            map.put(RET, user);
        }
        return map;
    }

    /**
     * 为保持数据库不为null,对参数进行处理
     *
     * @param user
     */
    private User setUserDellNull(User user) {
        user.setEmail(Tool.dellNull(user.getEmail()));
        user.setHead(Tool.dellNull(user.getHead()));
        user.setPhone(Tool.dellNull(user.getPhone()));
        user.setPassword(Tool.dellNull(user.getPassword()));
        user.setSex(Tool.dellNull(user.getSex()));
        user.setUser_name(Tool.dellNull(user.getUser_name()));
        user.setUser_profile(Tool.dellNull(user.getUser_profile()));
        user.setLogin_ip(Tool.dellNull(user.getLogin_ip()));

        user.setIsadmin(Tool.parseInt(user.getIsadmin()));
        user.setIsfrozen(Tool.parseInt(user.getIsfrozen()));
        user.setIsLogin(Tool.parseInt(user.getIsLogin()));
        user.setIsvip(Tool.parseInt(user.getIsvip()));

        return user;
    }

    /**
     * 加载用户图片
     *
     * @return
     */
    public Map<String, Object> findUserPic(Integer page) {
        if (page == null) {
            throw new RuntimeException(ERRMSG);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<Picture> piclist = pictureMapper.findUserPic(page, PICNUM);
        if (piclist != null) {
            if (PICNUM > piclist.size()) {
                piclist.addAll(pictureMapper.findUserPic(0,
                        PICNUM - piclist.size()));
            }
            map.put(FLAG, TRUE);
            map.put(RET, piclist);
        } else {
            map.put(FLAG, FALSE);
        }
        return map;
    }

    /**
     * 用户名检查
     *
     * @param username
     * @return
     */
    public Map<String, Object> findByUserName(String username) {
        if (username == null) {
            throw new RuntimeException(ERRMSG);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userMapper.findByUserName(username);
        if (user == null) {
            map.put(FLAG, TRUE);
        } else {
            map.put(FLAG, FALSE);
        }
        return map;
    }

    /**
     * 加载主页 图片
     *
     * @param page
     * @param pagenum
     * @return
     */
    public Map<String, Object> findMainPic(Integer page, Integer pagenum) {
        if (page == null) {
            throw new RuntimeException(ERRMSG);
        }
        if (pagenum == null) {
            pagenum = PICMAINNUM;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        List<Picture> piclist = pictureMapper.findMainPic(page, pagenum);
        if (piclist != null) {
            map.put(FLAG, TRUE);
            map.put(RET, piclist);
        } else {
            map.put(FLAG, FALSE);
        }
        return map;
    }

    /***
     * 登陆验证
     *
     * @param username
     * @param pwd
     * @return
     */
    public Map<String, Object> checkLogin(String username, String pwd) {
        if (username == null || pwd == null) {
            throw new RuntimeException(ERRMSG);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        User user = userMapper.findByUserName(username);
        if (user == null) {
            map.put(FLAG, NAMEERROR);
        } else if (Tool.comparePwd(pwd, user.getPassword())) {
            log.info("checkLogin:" + user.toString());
            map.put(FLAG, SUCCESS);
            map.put(RET, user);
            if (WHETHERNO.equals(user.getIsLogin())
                    && WHETHERNO.equals(user.getIsfrozen())) {
                // user.setIsLogin(WHETHERYSE);
                // userMapper.update(user);
                map.put(FLAG, SUCCESS);
                map.put(RET, user);
            } else if (WHETHERYSE.equals(user.getIsfrozen())) {
                map.put(FLAG, ISFROZEN);
            } else if (WHETHERYSE.equals(user.getIsLogin())) {
                map.put(FLAG, ISLOGIN);
            }
        } else {
            map.put(FLAG, PWDERROR);
        }
        return map;
    }

    /**
     * 登出
     *
     * @param user
     * @return
     */
    public Map<String, Object> loginOut(User user) {
        if (user == null) {
            throw new RuntimeException(ERRMSG);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        user.setIsLogin(WHETHERNO);
        userMapper.update(user);
        map.put(FLAG, TRUE);
        return map;
    }

}
